package com.muyu.danmo.service.impl;


import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.danmo.domain.DanmoFamousCard;
import com.muyu.danmo.domain.DanmoFamousPicture;
import com.muyu.danmo.mapper.DanmoFamousPictureMapper;
import com.muyu.danmo.service.IDanmoFamousPictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 名帖字库图片Service业务层处理
 *
 * @author yangle
 * @date 2024-09-23
 */
@Service
public class DanmoFamousPictureServiceImpl implements IDanmoFamousPictureService
{
    @Autowired
    private DanmoFamousPictureMapper danmoFamousPictureMapper;

    /**
     * 查询名帖字库图片
     *
     * @param id 名帖字库图片主键
     * @return 名帖字库图片
     */
    @Override
    public DanmoFamousPicture selectDanmoFamousPictureById(Long id)
    {
        return danmoFamousPictureMapper.selectDanmoFamousPictureById(id);
    }

    /**
     * 查询名帖字库图片列表
     *
     * @param danmoFamousPicture 名帖字库图片
     * @return 名帖字库图片
     */
    @Override
    public List<DanmoFamousPicture> selectDanmoFamousPictureList(DanmoFamousPicture danmoFamousPicture)
    {
        return danmoFamousPictureMapper.selectDanmoFamousPictureList(danmoFamousPicture);
    }

    /**
     * 新增名帖字库图片
     *
     * @param danmoFamousPicture 名帖字库图片
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDanmoFamousPicture(DanmoFamousPicture danmoFamousPicture)
    {
        danmoFamousPicture.setCreateTime(DateUtils.getNowDate());
        int rows = danmoFamousPictureMapper.insertDanmoFamousPicture(danmoFamousPicture);
        insertDanmoFamousCard(danmoFamousPicture);
        return rows;
    }

    /**
     * 修改名帖字库图片
     *
     * @param danmoFamousPicture 名帖字库图片
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDanmoFamousPicture(DanmoFamousPicture danmoFamousPicture)
    {
        danmoFamousPicture.setUpdateTime(DateUtils.getNowDate());
        danmoFamousPictureMapper.deleteDanmoFamousCardById(danmoFamousPicture.getId());
        insertDanmoFamousCard(danmoFamousPicture);
        return danmoFamousPictureMapper.updateDanmoFamousPicture(danmoFamousPicture);
    }

    /**
     * 批量删除名帖字库图片
     *
     * @param ids 需要删除的名帖字库图片主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoFamousPictureByIds(Long[] ids)
    {
        danmoFamousPictureMapper.deleteDanmoFamousCardByIds(ids);
        return danmoFamousPictureMapper.deleteDanmoFamousPictureByIds(ids);
    }

    /**
     * 删除名帖字库图片信息
     *
     * @param id 名帖字库图片主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoFamousPictureById(Long id)
    {
        danmoFamousPictureMapper.deleteDanmoFamousCardById(id);
        return danmoFamousPictureMapper.deleteDanmoFamousPictureById(id);
    }



    /**
     * 新增名帖字库信息
     *
     * @param danmoFamousPicture 名帖字库图片对象
     */
    public void insertDanmoFamousCard(DanmoFamousPicture danmoFamousPicture)
    {
        List<DanmoFamousCard> danmoFamousCardList = danmoFamousPicture.getDanmoFamousCardList();
        Long id = danmoFamousPicture.getId();
        if (StringUtils.isNotNull(danmoFamousCardList))
        {
            List<DanmoFamousCard> list = new ArrayList<DanmoFamousCard>();
            for (DanmoFamousCard danmoFamousCard : danmoFamousCardList)
            {
                danmoFamousCard.setId(id);
                list.add(danmoFamousCard);
            }
            if (list.size() > 0)
            {
                danmoFamousPictureMapper.batchDanmoFamousCard(list);
            }
        }
    }
}
